﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace NewRen.Models.ViewModel
{
    public class vProduct
    {
        public int ID { get; set; }


        public string ProductName { get; set; }


        public string Description { get; set; }


        public decimal Price { get; set; }

        public int Integration { get; set; }


        public DateTime Time { get; set; }


        public DateTime Begin { get; set; }

        public DateTime End { get; set; }

        public bool IsShow { get; set; }

        public int BusinessID { get; set; }

        public string BusinessName { set; get; }

        public bool IsPass { get; set; }

        public List<Images> Images { get; set; }

        public string Cover { set; get; }

        public ProductStatu Statu { set; get; }


        public string RemainingTime { get; set; }
        public vProduct() { }

        public vProduct(Product model)
        {
            DB db = new DB();
            this.ID = model.ID;
            this.ProductName = model.ProductName;
            this.Description = model.Description;
            this.Price = model.Price;
            this.Integration = model.Integration;
            this.Time = model.Time;
            this.Begin = model.Begin;
            this.End = model.End;
            this.IsShow = model.IsShow;
            this.BusinessID = model.BusinessID;
            this.IsPass = model.IsPass;
            this.BusinessName = db.Businesses.Find(model.BusinessID).BusinessName;
            List<Images> images = db.Images.Where(c => c.TID == model.ID).Where(c => c.ImageType == ImageType.Product).ToList();
            this.Images = images;
            this.Cover = (images.Count == 0) ? "/Images/userphoto.png" : images[0].Path;
           
            if (DateTime.Now < model.Begin)
            {
                this.Statu = ProductStatu.Coming;
                var diff = model.End - DateTime.Now;
                this.RemainingTime = string.Format("{0}天{1}小时{2}分{3}秒", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
            }
            else if (DateTime.Now > model.End)
            {
                this.Statu = ProductStatu.Finished;
                var diff = model.End - model.End;
                this.RemainingTime = string.Format("{0}天{1}小时{2}分{3}秒", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
            }
            else if (DateTime.Now >= model.Begin && DateTime.Now <= model.End)
            {
                this.Statu = ProductStatu.Running;
                var diff = model.End - DateTime.Now;
                this.RemainingTime = string.Format("{0}天{1}小时{2}分{3}秒", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
            }
        }
    }
}